package com.example.administrator.mobilesafe.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.TelephonyManager;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.mobilesafe.utils.NumberAddressQuery;
import com.example.administrator.mobliesafe.R;

import java.util.Map;
import java.util.Set;

public class PhoneAddressService extends Service {
    private TelephonyManager telephonyManager;
    private MyListenerPhone phoneStateListener;
    private OutCallAddressReceiver outCallAddressReceiver;

    private WindowManager windowManager;
    private View view;

    public PhoneAddressService() {
    }

    class OutCallAddressReceiver extends BroadcastReceiver {
        public OutCallAddressReceiver() {
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            String number = getResultData();
            String address  = NumberAddressQuery.queryNumberAddress(number);
            myToast(address);
        };
    }

    private void myToast(String address) {
        view = View.inflate(this, R.layout.address_show,null);
        TextView textView = (TextView)view.findViewById(R.id.tv_address);

        int [] ids = {R.drawable.call_locate_white,R.drawable.call_locate_orange,R.drawable.call_locate_blue
                ,R.drawable.call_locate_gray,R.drawable.call_locate_green};

        SharedPreferences sharedPreferences = getSharedPreferences("config",MODE_PRIVATE);

        int color = sharedPreferences.getInt("which",0);
        view.setBackgroundResource(ids[color]);
        textView.setText(address);

        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE|
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
        layoutParams.format = PixelFormat.TRANSLUCENT;
        layoutParams.type = WindowManager.LayoutParams.TYPE_TOAST;
        windowManager.addView(view,layoutParams);

    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    private class MyListenerPhone extends PhoneStateListener{
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            switch (state){
                case TelephonyManager.CALL_STATE_RINGING:
                    String address = NumberAddressQuery.queryNumberAddress(incomingNumber);
                    myToast(address);
                    break;
                case TelephonyManager.CALL_STATE_IDLE:
                    if(view!= null)
                        windowManager.removeView(view);
                    break;
                default:break;
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
        phoneStateListener = null;
        unregisterReceiver(outCallAddressReceiver);
        outCallAddressReceiver = null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        telephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
        phoneStateListener = new MyListenerPhone();
        telephonyManager.listen(phoneStateListener,PhoneStateListener.LISTEN_CALL_STATE);
        outCallAddressReceiver = new OutCallAddressReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.intent.action.NEW_OUTGOING_CALL");
        registerReceiver(outCallAddressReceiver, intentFilter);

        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    }
}
